<template>
  <a-card :bordered="false" style="height: 90%">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="名称">
              <a-input v-model="queryParam.label" placeholder="名称"/>
            </a-form-item>
          </a-col>

          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span
              class="table-page-search-submitButtons"
              :style="advanced && { float: 'right', overflow: 'hidden' } || {} "
            >
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 13px" @click="queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>

      </a-form>
    </div>

    <div class="table-operator">
      <a-button v-if="$auth('sys_dict_add')" type="primary" icon="plus" @click="openEditDialog()">新建</a-button>
    </div>

    <s-table
      ref="table"
      size="middle"
      :row-key="record => record.id"
      :columns="columns"
      :data="loadData"
      show-pagination="auto"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <div>
            <a-button v-if="$auth('sys_dict_add')" v-show="record.parentId == null" icon="plus" type="primary" style="margin-right: 10px"
                      @click="newEditDialog(record)">新建</a-button>
            <a-button v-if="$auth('sys_dict_edit')" type="primary" style="margin-right: 10px" @click="openEditDialog(record)">编辑</a-button>
            <a-button v-if="$auth('sys_dict_del')" type="danger" @click="deleteDict(record.id)">删除</a-button>
          </div>
        </template>
      </span>
    </s-table>

    <dict-edit ref="menuEdit" @close="handleOk"/>
  </a-card>
</template>
<style scoped lang="scss">
  .table-operator {
    margin-bottom: 10px;
  }
</style>
<script>
  import DictEdit from '@/views/sys/dict/Edit'
  import {STable} from '@/components'
  import {listDict, deleteDict} from '@/api/dict'

  export default {
    name: 'Index',
    components: {
      STable,
      DictEdit
    },
    data() {
      return {
        mdl: {},
        // 高级搜索 展开/关闭
        advanced: false,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '名称',
            dataIndex: 'label',
            key: 'label',
            width: '150px'
          },
          {
            title: '编号',
            dataIndex: 'id',
            key: 'id',
            width: '150px'
          },
          {
            title: '编码',
            dataIndex: 'code',
            key: 'code',
            width: '150px'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: '350px',
            scopedSlots: {customRender: 'action'}
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return listDict(Object.assign(parameter, this.queryParam))
            .then(res => {
              res.data.content = this.deleteEmptyChildren(res.data.content)
              return res.data
            })
        },
        selectedRowKeys: [],
        selectedRows: [],
        optionAlertShow: false
      }
    },
    created() {
    },
    methods: {
      deleteDict(id) {
        const _this = this
        _this.$confirm({
          title: '确认删除该字典吗?',
          content: '删除后将不可恢复',
          okText: '确定',
          okType: 'danger',
          cancelText: '取消',
          onOk() {
            return deleteDict(id).then(res => {
              if (res.code === '0') {
                _this.$message.success('删除成功')
              } else {
                _this.$message.error(res.message)
              }
              _this.handleOk()
            })
          },
          onCancel() {
          }
        })
      },
      openEditDialog(obj) {
        var form = Object.assign({}, obj)
        this.$refs.menuEdit.initData(form)
      },
      newEditDialog(obj) {
        var form = {parentId: obj.id}
        this.$refs.menuEdit.initData(form)
      },
      deleteEmptyChildren(data) {
        if (data.length > 0) {
          for (const i in data) {
            if (data[i].children && data[i].children.length) {
              this.deleteEmptyChildren(data[i].children)
            } else {
              delete data[i].children
            }
          }
        }
        return data
      },
      handleOk() {
        this.$refs.table.refresh(true)
      }
    }
  }
</script>
